<template>
	<el-dialog v-model="dialogFormVisible" title="意见" width="700" :close-on-click-modal="false" :show-close="false">
		<HyForm ref="hyFormRef" :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" />
    <div>
      <p style="margin-bottom:10px;font-weight:bold;">附件上传：</p>
      <el-upload
        v-model:file-list="fileList"
        class="upload-demo"
        :action="baseURL + '/admin/sys-file/upload'"
        multiple
        :before-upload="beforeUpload"
        :limit="3"
				:on-exceed="(files,uploadFiles)=>handleExceed(files,uploadFiles,3)"
        accept=".jpg,.doc,.docx,.pdf,.jpeg,.png"
        :headers="headers"
      >
        <el-button type="primary" icon="Upload">上传附件</el-button>
        <template #tip>
          <div class="el-upload__tip">
            仅支持上传jpg/jpeg/doc/docx/pdf/png格式文件，文件不能超过200MB
          </div>
        </template>
      </el-upload>
    </div>
    <template #footer>
			<div class="dialog-footer">
				<el-button @click="handleCancel">取消</el-button>
				<el-button type="primary" @click="confirm">确定</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script lang="ts">
import { defineComponent,computed,toRaw } from 'vue';
import HyForm from '/@/components/HyForm/index.vue';
import Upload from '/@/components/Upload/index.vue';
import { Session } from '/@/utils/storage';
import { ElMessage } from 'element-plus';
import { spotSave } from '/@/api/acceptance'
export default defineComponent({
	name: 'Advice',
	components: { HyForm,Upload },
	props: {
		dialogFormVisible: {
			type: Boolean,
			default: true,
		},
		hyFormFields: {
			type: Object,
			default: () => {},
		},
    hyFormConfig: {
			type: Object,
			default: () => {},
		},
		hyFormDefault: {
			type: Object,
			default: () => {},
		},
		appCode:{
			type:String,
			default:''
		},
		declareId:{
			type:String,
			default:''
		},
		implementationId:{
			type:String,
			default:''
		},
	},
	setup(props,{ emit }) {
    const fileList = ref<UploadUserFile[]>([])
		const hyFormRef = ref(null);
		function confirm() {
			hyFormRef.value.check().then(valid=>{
				console.log(valid)
				if(valid){
					let arr = []
					fileList.value.forEach((item) => {
						if (item.response && item.response.ok) {
							let json = {
								name: item.response.data?.name,
								url: item.response.data?.url,
							};
							arr.push(json);
						}
					});
					console.log(props.appCode)
					console.log(hyFormRef.value.hyForm)
					let params = {
						...toRaw(hyFormRef.value.hyForm),
						appCode:props.appCode,
						declareId:props.declareId,
						implementationId:props.implementationId,
						appDeclareFile:arr,
					}
					console.log(params)
					// return;
					spotSave(params).then(res=>{
						if(res.ok){
							ElMessage.success('操作成功')
							handleCancel(1)
						}else{
							ElMessage.error(res.msg)
						}
					})
				}
			})
		}
    function handleCancel(flag){
      emit('handleCancel',flag)
    }
		const headers = computed(() => {
			return {
				Authorization: 'Bearer ' + Session.get('token'),
				'TENANT-ID': Session.getTenant(),
			};
		});
    
		function handleExceed(files,uploadFiles,limit){
			ElMessage.warning(`最多上传${limit}个文件!`)
		}
		function beforeUpload(file) {
			const { size, type } = file;
			const maxSize = 1024 * 1024 * 200; // 例如，限制为 200MB
			const accept = '.jpg,.doc,.docx,.pdf,.jpeg,.png';
			const fileType = accept.split(',')
			// const fileType = []
			if (fileType.length) {
				const fileName = file.name.split('.');
				const fileExt = `.${fileName[fileName.length - 1]}`;
				const isTypeOk = fileType.includes(fileExt);
				if (!isTypeOk) {
					ElMessage.error(`文件格式错误，请上传 ${fileType.join(',')} 格式的文件`);
					return false;
				}
			}
			if (size > maxSize) {
				ElMessage.error('文件大小超过限制，请选择小于 200MB 的文件');
				return false;
			}
			return true;
		}
		return {
      fileList,
			hyFormRef,
      headers,
			handleExceed,
			confirm,
      handleCancel,
      beforeUpload,
		};
	},
});
</script>
